package club.xinghe.servlet.student;
import club.xinghe.biz.AdminBiz;
import club.xinghe.biz.StudentBiz;
import club.xinghe.biz.TeacherBiz;
import club.xinghe.entity.Admin;
import club.xinghe.entity.Student;
import club.xinghe.entity.Teacher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 说明:三合一登录验证
 *
 * @姚文斌
 * @create 2019-10-08 上午 10:23
 */
@WebServlet("/AllLoginServlet")
public class AllLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //写业务处理代码
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();

        String role=request.getParameter("role");
        int zhanghao= Integer.parseInt(request.getParameter("zhanghao"));
        String password=request.getParameter("password");
        //System.out.println(role+"进啦");

        switch (role){
            case "学生":{
                Student s=new Student(zhanghao,password);
                StudentBiz studentBiz=new StudentBiz();
                Student student=studentBiz.studentLogin(s);
                if (student==null){
                    out.print("fail");
                }else {
                    HttpSession session=request.getSession();
                    session.setAttribute("student",student);
                    out.print("student");
                }

            }
                break;
            case "管理员":{
                Admin a=new Admin(zhanghao,password);

                AdminBiz adminBiz=new AdminBiz();
                Admin admin= adminBiz.adminLogin(a);
                if (admin==null){
                    out.print("fail");
                }else {
                    HttpSession session=request.getSession();
                    session.setAttribute("admin",admin);
                    out.print("admin");
                }

            }
                break;
            case "老师":{
                Teacher t=new Teacher(zhanghao,password);
                TeacherBiz teacherBiz=new TeacherBiz();
                Teacher teacher=teacherBiz.teacherLogin(t);
                if (teacher==null){
                    out.print("fail");
                }else {
                    HttpSession session=request.getSession();
                    session.setAttribute("teacher",teacher);
                    out.print("teacher");
                }

            }
                break;
            default:break;
        }

    }
}
